package com.example.listgame.fragment;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.listgame.R;

public class RankingActivity extends AppCompatActivity {

    private SimpleFragment simpleFragment;
    private MediumFragment mediumFragment;
    private DifficultFragment difficultFragment;
    private Button mSimple,mMedium,mDifficult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);

        //实例化SimpleFragment
        simpleFragment = new SimpleFragment();
        //将SimpleFragment添加到Activity中
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,simpleFragment).commitAllowingStateLoss();

        mSimple = findViewById(R.id.simple);
        mMedium = findViewById(R.id.medium);
        mDifficult = findViewById(R.id.difficult);
        OnClick onClick = new OnClick();
        mSimple.setOnClickListener(onClick);
        mMedium.setOnClickListener(onClick);
        mDifficult.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.simple:
                    if (simpleFragment == null){
                        simpleFragment = new SimpleFragment();
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment,simpleFragment).commitAllowingStateLoss();
                    break;
                case R.id.medium:
                    if (mediumFragment == null){
                        mediumFragment = new MediumFragment();
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment,mediumFragment).commitAllowingStateLoss();
                    break;
                case R.id.difficult:
                    if (difficultFragment == null){
                        difficultFragment = new DifficultFragment();
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment,difficultFragment).commitAllowingStateLoss();
                    break;
            }
        }
    }
}
